//
//  KDCameraView.m
//  LaiKeDaoClient
//
//  Created by dongkedi的iMac on 15/11/10.
//  Copyright © 2015年 LKD. All rights reserved.
//

#import "KDCameraView.h"

@implementation KDCameraView

-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}

- (void)showCameraView:(UIViewController *)superController
{
    self.superController = superController;
    [self showCameraView];
}

- (void)showCameraView{
    // 判断是否支持相机
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
        sheet  = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"从相册选择", nil];
    }else {
        sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"从相册选择", nil];
    }
    sheet.tag = 255;
    [sheet showInView:self.superController.view];
}
#pragma mark - actionsheet delegate
-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (actionSheet.cancelButtonIndex == buttonIndex) {
        return;
    }
    if (actionSheet.tag == 255) {
        NSUInteger sourceType = 0;
        // 判断是否支持相机
        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            switch (buttonIndex) {
                case 0:    // 相机
                    sourceType = UIImagePickerControllerSourceTypeCamera;
                    break;
                case 1:    // 相册
                    sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                    break;
            }
        }else {
            sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        }
        // 跳转到相机或相册页面
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        
        if (1==buttonIndex) {
            UIView* statusView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.superController.view.width, 20)];
            statusView.backgroundColor = [UIColor clearColor];
            [imagePickerController.view addSubview:statusView];
        }
        imagePickerController.delegate = self;
        // imagePickerController.allowsEditing = YES;
        imagePickerController.sourceType = sourceType;
        if([[[UIDevice
              currentDevice] systemVersion] floatValue]>=8.0) {
            
            self.superController.modalPresentationStyle=UIModalPresentationOverCurrentContext;
            
        }
        [self performSelector:@selector(presentCameraView:) withObject:imagePickerController afterDelay:1.0f];
        //[self presentViewController:imagePickerController animated:YES completion:^{}];
    }
}
-(void)presentCameraView:(UIImagePickerController*)imagePickerController{
    [self.superController presentViewController:imagePickerController animated:YES completion:nil];
}
#pragma mark - image picker delegte
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:YES completion:^{}];
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    if ([self.cameraViewDelegate respondsToSelector:@selector(chooseImage:)]) {
        [self.cameraViewDelegate chooseImage:image];
    }
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self.superController dismissViewControllerAnimated:YES completion:^{}];
}

@end
